package samsung.sds.survey.surveyManagement.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import samsung.sds.survey.surveyManagement.dao.SurveyDesignMapper;
import samsung.sds.survey.surveyManagement.model.OptionEntity;
import samsung.sds.survey.surveyManagement.model.QuestionEntity;
import samsung.sds.survey.surveyManagement.model.QuestionModel;
import samsung.sds.survey.surveyManagement.model.SurveyEntity;
import samsung.sds.survey.surveyManagement.service.SurveyDesignService;
import samsung.sds.system.common.baseQuery.AbstractBaseService;
import samsung.sds.system.utils.CommonTools;

/**
 * @author sp.deng
 * @createDate 2016-01-13
 * 
 * 问题编辑操作业务实现类
 */
@Service
public class SurveyDesignServiceImpl extends AbstractBaseService<SurveyEntity> 
	implements SurveyDesignService {

	@Autowired
	private SurveyDesignMapper surveyDesignMapper;

	@Override
	protected SurveyDesignMapper getMapper() {
		return surveyDesignMapper;
	}

	/**
	 * 查询问题及选项
	 * @param survey_Id 问卷id
	 * @return 问题的集合
	 */
	public List<QuestionEntity> getQuestionEntity(String surveyId) {
		List<QuestionEntity> qlist = this.getMapper().selectTF_F_QUESTION(surveyId);
		List<QuestionEntity> qlist1 = new ArrayList<QuestionEntity>();
		for (QuestionEntity questionEntity : qlist) {
			//把查询得到的选项的集合封装到相应的问题
			List<OptionEntity> olist = this.getMapper().selectTF_F_QUESTION_OPTIONITEMT(questionEntity.getId());
			questionEntity.setQuestionOptionList(olist);
			qlist1.add(questionEntity);
		}
		 return qlist;
	}

	/**
	 * 删除问题选项
	 * @param id 问题id
	 * @return 成功返回1
	 */
	public int deleteOption(String id) {
		return this.getMapper().deleteTF_F_QUESTION_OPTIONITEMT(id);
	}

	/**
	 * 插入问题 
	 * @param questionEntity 问题实体
	 * @return 成功返回1
	 */
	public int insertQuestion(QuestionEntity questionEntity) {
		questionEntity.setLastModifyTime(CommonTools.getCurrentDateTime());
		return this.getMapper().insertTF_F_QUESTION(questionEntity);
	}

	/**
	 * 插入问题的选项
	 * @param optionEntity 选项实体
	 * @return 成功返回1
	 */
	public int insertOption(OptionEntity optionEntity) {
		optionEntity.setLastModifyTime(CommonTools.getCurrentDateTime());
		return this.getMapper().insertTF_F_QUESTION_OPTIONITEMT(optionEntity);
	}

	/**
	 * 问题编辑
	 * @param surveyEntity 问卷实体
	 * @param qlist 问题及选项的集合
	 * @return 成功返回true 失败返回false
	 */
	public boolean saveQuestion(SurveyEntity surveyEntity, QuestionModel qlist) {
		boolean flag = true ;
		//删除选项
		List<QuestionEntity> questions = qlist.getQuestionList();
		
		//if ("0".equals(surveyEntity.getSurvey_Type())) {
			// 问卷类型为普通时，并且问题类型为单选和多选
			for (QuestionEntity question : questions) { // 遍历页面传过来的问题
				if (question.getId() != null) {
					List<QuestionEntity> questionlist = this.getQuestionEntity(question.getSurveyId());
					for (QuestionEntity q : questionlist) {
						if (q.getQuestionOptionList().size() != 0) { // 问题有选项的时候
							int deleteOption = this.deleteOption(question.getId()); // 删除所有选项
							if (deleteOption < 0) {
								flag = false;
							}
						}
					}
				}
			}
		//}
		//删除问题
		int deleteQuestion = this.deleteEntity(surveyEntity);// 删除所有问题
		if (deleteQuestion < 0) {
			flag = false;
		}
		// 插入问题
		for (QuestionEntity question : questions) { // 遍历页面传过来的问题
			if (question.getQuestionContent() != null) {
				List<OptionEntity> options = question.getQuestionOptionList();
				question.setSurveyId(surveyEntity.getSurvey_Id());
				question.setId(CommonTools.getKeyUUID());//随机生成id
				//新增加的问题
				if (question.getCreateUser() == null) {
					question.setCreateUser(CommonTools.getSessionUserName());
				}
				if (question.getCreateTime() == null) {
					question.setCreateTime(CommonTools.getCurrentDateTime());
				}
				question.setLastModifyUser(CommonTools.getSessionUserName());
				this.insertQuestion(question);
				
				// 插入选项
				//if ("0".equals(surveyEntity.getSurvey_Type())) {// 问卷类型为普通
					if ("1".equals(question.getQuestionType())
							|| "2".equals(question.getQuestionType())) {// 问题类型为单选或多选
						for (OptionEntity option : options) {// 遍历页面传进来的选项
							option.setQuestionId(question.getId());
							//新增加的选项
							if (option.getCreateUser() == null) {
								option.setCreateUser(CommonTools.getSessionUserName());
							}
							if (option.getCreateTime() == null) {
								option.setCreateTime(CommonTools.getCurrentDateTime());
							}
							option.setLastModifyUser(CommonTools.getSessionUserName());
							int insertOption = this.insertOption(option);
							if (insertOption < 0) {
								flag = false;
							}
						}
					}
				//}
			}
		}
	
		//更新问卷
		surveyEntity.setLast_Modify_Time(CommonTools.getCurrentDateTime());
		surveyEntity.setLast_Modify_User(CommonTools.getSessionUserName());
		this.getMapper().updateEntity(surveyEntity);
		return flag;
	}	
}
